package com.devnn.view;


import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.view.Window;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.devnn.base.R;


public class InputDialog extends Dialog implements View.OnClickListener {
    private Button btnOK, btnCancel;
    private TextView tvMessage;
    private Context context;
    private EditText etInput;
    private OnInputSureListener onInputSureListener;

    public interface OnInputSureListener {
        void onInputSure(String text);

        void onInputCancel();
    }

    public void setOnInputSureListener(OnInputSureListener onInputSureListener) {
        this.onInputSureListener = onInputSureListener;
    }

    public InputDialog(Context context) {
        super(context);
        this.context = context;
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        // TODO Auto-generated constructor stub
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_dialog_input);
        getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        this.getWindow().setLayout(context.getResources().getDisplayMetrics().widthPixels - 40, LayoutParams.WRAP_CONTENT);
        initView();
        setEvent();
    }

    private void initView() {
        tvMessage = (TextView) findViewById(R.id.confirm_message);
        btnOK = (Button) findViewById(R.id.confirm_ok);
        btnCancel = (Button) findViewById(R.id.confirm_cancel);
        etInput = (EditText) findViewById(R.id.input_text);
        etInput.setInputType(InputType.TYPE_CLASS_TEXT);

    }

    public void setInputType(int type) {
        etInput.setInputType(type);
    }

    private void setEvent() {
        btnOK.setOnClickListener(this);
        btnCancel.setOnClickListener(this);
    }

    public void setMessage(String msg) {
        tvMessage.setText(msg);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if (v.equals(this.btnCancel)) {
            if (this.onInputSureListener != null) {
                this.onInputSureListener.onInputCancel();
            }

        } else if (v.equals(this.btnOK)) {
            if(etInput.getText().toString().trim().length()==0){
                etInput.setError("请输入内容");
                return;
            }
            if (this.onInputSureListener != null) {
                this.onInputSureListener.onInputSure(etInput.getText().toString());
            }
        }
        this.dismiss();
    }
}
